package OneKeyMakeForm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.wtframework.platform.server.util.FilePath;



/** 
* @author 卢俊华 
* @version 创建时间：2018年8月15日 下午3:28:33 
* @Description 
* @param
*/
public class OneKeyMakeForm {
	
	private static String project;//项目
	
	private static String module;//模块
	
	private static String key;//用于url和各文件的关键字
	
	private static String controller;//controller的名字，默认使用key

	private static String bigKey = "";//用于url和各文件的关键字首字母大写
	
	private static String searchText = null;//设置搜索对象的哪一个字段（填字段名）
	
	private static String theKey = null;//设置这个对象的主键
	
	private static String coreConfigUrl = null;//如D:/eclipseWorkSpace/yjy_xmgl//src/org/xmgl/core
	
	private static String jetxConfigUrl = null;//如D:/eclipseWorkSpace/yjy_xmgl//src/org/xmgl/core
	
	private static String styleConfigUrl = null;//如D:/eclipseWorkSpace/yjy_xmgl//src/org/xmgl/core

	
	//demo文件路径
	private static String jetxUrl = FilePath.getSystemRootPath().replace("/war", "")+"/OneKeyMakeForm/demo/demo.jetx";
	private static String jsUrl = FilePath.getSystemRootPath().replace("/war", "")+"/OneKeyMakeForm/demo/demo.js";
	private static String cssUrl = FilePath.getSystemRootPath().replace("/war", "")+"/OneKeyMakeForm/demo/demo.css";
	private static String controllerUrl = FilePath.getSystemRootPath().replace("/war", "")+"/OneKeyMakeForm/demo/DemoController.txt";
	private static String managerUrl = FilePath.getSystemRootPath().replace("/war", "")+"/OneKeyMakeForm/demo/DemoManager.txt";
	private static String serveiceUrl = FilePath.getSystemRootPath().replace("/war", "")+"/OneKeyMakeForm/demo/DemoService.txt";
	
	
	
	
	class  SingleEle {
		
		String name = "";//中文名
		String attrName = "";//属性名（要是类里面有的属性名称）
		String type = "";//类型  input hide(隐藏的input) textarea date select null(空着到时候自己写)
		Boolean isKey = false;//是否为主键
		
		//select的属性
		String optionClass = "";//下拉对象的类型 
		
		String optionId = "";//下拉对象的id的属性名
		
		String optionValue = "";//下拉对象的value的属性名
		
		String condition = "";//查询条件
		
		
		
		public SingleEle(String name,String attrName,String type){
			this.name = name;
			this.attrName = attrName;
			this.type = type;
		}
		public SingleEle(String name,String attrName,String type,Boolean isKey){
			this.name = name;
			this.attrName = attrName;
			this.type = type;
			this.isKey = isKey;
		}

		public SingleEle(String name, String attrName, String type,
				String optionClass, String optionId, String optionValue,
				String condition) {
			super();
			this.name = name;
			this.attrName = attrName;
			this.type = type;
			this.optionClass = optionClass;
			this.optionId = optionId;
			this.optionValue = optionValue;
			this.condition = condition;
		}
		public SingleEle(String name, String attrName, String type,
				String optionClass, String optionId, String optionValue,
				String condition, Boolean isKey) {
			super();
			this.name = name;
			this.attrName = attrName;
			this.type = type;
			this.optionClass = optionClass;
			this.optionId = optionId;
			this.optionValue = optionValue;
			this.condition = condition;
			this.isKey = isKey;
		}

		
		
	}
	
	public  SingleEle getSingle(String name, String attrName, String type,
			String optionClass, String optionId, String optionValue,
			String condition,Boolean isKey){
		
		return new SingleEle(name, attrName, type, optionClass, optionId, optionValue, condition,isKey);
	}
	
	
	public DoubleEle getDouble(SingleEle singleLeft,SingleEle singleRight){
		
		return new DoubleEle(singleLeft, singleRight);
	}
	
	class DoubleEle {
		
		SingleEle singleLeft;//左边的
		SingleEle singleRight;//右边的
		
		public DoubleEle(SingleEle singleLeft,SingleEle singleRight){
			this.singleLeft = singleLeft;
			this.singleRight = singleRight;
		}
		
	}
	
	/** 
	* @author 卢俊华 
	* @version 创建时间：2018年8月7日 上午11:39:45 
	* @Description 读文件，返回文件的string
	* @param
	*/ 
	private static String getFileString(String url){
		try {
			
			File file = new File(url);
			FileInputStream inputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader  = new InputStreamReader(inputStream);
			BufferedReader reader=new BufferedReader(inputStreamReader);
			StringBuilder sb=new StringBuilder();
			String line=null;
			while((line=reader.readLine())!=null){
			//保留换行符
			sb.append(line+"\n");
			}
			reader.close();
			//System.out.println(sb.toString());
			
			return sb.toString();
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/** 
	* @author 卢俊华 
	* @version 创建时间：2018年8月7日 上午11:40:00 
	* @Description 新建文件，写进去
	* @param
	*/ 
	private static void writeFile(String url,String text) throws Exception{
		
		File file = new File(url);
		File path = file.getParentFile();
		//将内容输出到文件
		//创建目录
		if(!path.exists()){
			path.mkdirs();
		}
		if(!file.exists()){
			file.createNewFile();
		}
		FileWriter writer=new FileWriter(file);
		writer.write(text);
		writer.flush();
		writer.close();
	}
	
	
	/** 
	 * @author 卢俊华 
	 * @version 创建时间：2018年8月7日 上午11:40:00 
	 * @Description 在文件里面追加controller
	 * @param
	 */ 
	private static void appendFile(String url) throws Exception{
		
		try {
			String orgText = OneKeyMakeForm.getFileString(url);
			
			//需要导入的类
			List<String> importList = new ArrayList<String>();
			importList.add("import java.util.ArrayList;");
			importList.add("import java.util.HashMap;");
			importList.add("import java.util.List;");
			importList.add("import java.util.Map;");
			importList.add("import org.openredhead.springx.core.server.page.IPage;");
			importList.add("import org.openredhead.springx.core.web.mvc.support.ActionValueAssistant;");
			importList.add("import org.springframework.beans.factory.annotation.Autowired;");
			importList.add("import org.springframework.stereotype.Controller;");
			importList.add("import org.springframework.ui.ModelMap;");
			importList.add("import org.springframework.web.bind.annotation.RequestMapping;");
			importList.add("import org.springframework.web.bind.annotation.ResponseBody;");
			importList.add("import org."+project+".core.server.manager.impl."+bigKey+"Manager;");
			importList.add("import org."+project+".core.server.model.impl."+bigKey+";");
			
			String attr = "\n\t private  String "+bigKey+"Url = \"/"+project+"/"+module+"\";\n\n"+
						  "\t @Autowired\n"+
						  "\t private "+bigKey+"Manager "+key+"Manager;\n";
			String theSearchCode = "";
			if(searchText!=null && !searchText.equals("")){
				theSearchCode = 	"\t  String name = actionValueAssistant.getString(\""+searchText+"\");\n"+
									"\t if("+searchText+"!=null && !"+searchText+".equals(\"\")){\n"+
									"\t hql.append(\" and "+searchText+" like ? \");\n"+
									"\t param.add('%'+"+searchText+"+'%');\n"+
									"\t }\n";
			}

			String controller = "\t @RequestMapping(\"/"+key+"\")\n"+
								"\t public String demo(ModelMap modelMap){\n"+
								"\t\t return "+bigKey+"Url+\"/"+key+"\";\n"+
								"\t }\n\n\n"+
								"\t @ResponseBody\n"+
								"\t @RequestMapping(\"/"+key+"/get"+bigKey+"List\")\n"+
								"\t public Map<String, Object> get"+bigKey+"List(ModelMap modelMap,Integer page,Integer limit, ActionValueAssistant actionValueAssistant){\n"+
								"\t Map<String, Object> ret = new HashMap<String, Object>();\n"+
								"\t StringBuffer hql = new StringBuffer();\n"+
								"\t List<Object> param = new ArrayList<Object>();\n"+
								"\t hql.append(\"from "+bigKey+" where status = 0 \");\n\n"+
								theSearchCode+"\n"+
								"\t IPage<"+bigKey+"> "+key+"Page = "+key+"Manager.findByQuery(hql.toString(),param.toArray()).getPageByPageNo(page, limit);\n"+
								"\t ret.put(\"data\" , "+key+"Page.getData());\n"+
								"\t ret.put(\"code\", 0);\n"+
								"\t ret.put(\"count\", "+key+"Page.getTotalCount());\n"+
								"\t return ret;\n"+
								"\t }\n\n\n"+
								" }\n\n";
			
			
			
			File file = new File(url);
			FileInputStream inputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader  = new InputStreamReader(inputStream);
			BufferedReader reader=new BufferedReader(inputStreamReader);
			StringBuilder sb=new StringBuilder();
			String line=null;
			
			int flag = 0;//标志
			while((line=reader.readLine())!=null){
				
			if(line.startsWith("package")){//导入的类
				sb.append(line+"\n\n");
				for(String imp : importList){//过滤重复的
					if(!orgText.contains(imp)){
						sb.append(imp+"\n");
					}
				}
			}else if(line.contains("class") && flag==0){//属性
				sb.append(line+"\n");
				sb.append(attr);
				flag++;
			}else{
				//保留换行符
				sb.append(line+"\n");
			}
			
			}
			String target = sb.toString();
			
			target = target.substring(0, target.lastIndexOf("}"))+controller;
			
			reader.close();
			
			
			
			File path = file.getParentFile();
			//将内容输出到文件
			//创建目录
			if(!path.exists()){
				path.mkdirs();
			}
			if(!file.exists()){
				file.createNewFile();
			}
			FileWriter writer=new FileWriter(file);
			writer.write(target);
			writer.flush();
			writer.close();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/** 
	* @author 卢俊华 
	* @version 创建时间：2018年8月8日 上午10:58:04 
	* @Description 返回form的元素
	* @param
	*/ 
	private static String getEle(Object ele){
		try {
			
	
		String singleEle = "\t\t\t\t\t\t<div class='layui-form-item'>\n"+
						   "\t\t\t\t\t\t\t<label class='layui-form-label'>theName：</label>\n"+
						   "\t\t\t\t\t\t\t<div class='layui-input-block'>\n"+
						   "\t\t\t\t\t\t\t\t<input id='theAttrName' name='theAttrName'  placeholder='请输入' autocomplete='off'  class='layui-input'>\n"+
						   "\t\t\t\t\t\t\t</div>\n"+
						   "\t\t\t\t\t\t</div>\n";
		
		String hide =	   "\t\t\t\t\t\t<input type='hidden' name='theAttrName' id='theAttrName' />\n";
		
		String doubleEle =  "\t\t\t\t\t<!-- 两列开始 -->\n"+
						    "\t\t\t\t\t<div class='layui-row'>\n"+
						    "\t\t\t\t\t\t<div class='layui-col-md6'>\n"+
						    "doubleLeft\n"+//左边
						    "\t\t\t\t\t\t</div>\n"+
						    "\t\t\t\t\t\t<div class='layui-col-md6'>\n"+
						    "doubleRight\n"+//右边
						    "\t\t\t\t\t\t</div>\n"+
						    "\t\t\t\t\t</div>\n"+
						    "\t\t\t\t\t<!-- 两列结束 -->\n";
		
		String date = "\t\t\t\t\t\t<div class='layui-form-item'>\n"+
					    "\t\t\t\t\t\t\t<label class='layui-form-label'>theName：</label>\n"+
					    "\t\t\t\t\t\t\t<div class='layui-input-block'>\n"+
					    "\t\t\t\t\t\t\t\t<input type='text' name='theAttrName' class='layui-input' id='theAttrName' placeholder='yyyy-MM-dd'>\n"+
					    "\t\t\t\t\t\t\t</div>\n"+
					    "\t\t\t\t\t\t</div>\n";
		
		String select = "\t\t\t\t\t\t<div class='layui-form-item'>\n"+
					    "\t\t\t\t\t\t\t<label class='layui-form-label'>theName：</label>\n"+
					    "\t\t\t\t\t\t\t<div class='layui-input-block'>\n"+
					    "\t\t\t\t\t\t\t\t<select name='theAttrName' id='theAttrName' lay-verify='title' lay-filter='level'>\n"+
					    "\t\t\t\t\t\t\t\t<option value=''></option>\n"+
					    "\t\t\t\t\t\t\t\t #for(optionClassBig optionClass :getList('optionClassManager',condition))\n"+
					    "\t\t\t\t\t\t\t\t <option value='\\\\\\${optionClass?.optionId\\\\}' >\\\\\\${optionClass?.optionValue\\\\}</option>\n"+
						"\t\t\t\t\t\t\t\t #end\n"+ 
						"\t\t\t\t\t\t\t\t</select>\n"+
					    "\t\t\t\t\t\t\t</div>\n"+
					    "\t\t\t\t\t\t</div>\n";
		
		String textarea =  "\t\t\t\t\t\t<div class='layui-form-item'>\n"+
						   "\t\t\t\t\t\t\t<label class='layui-form-label'>theName：</label>\n"+
						   "\t\t\t\t\t\t\t<div class='layui-input-block'>\n"+
						   "\t\t\t\t\t\t\t\t<textarea id='theAttrName' name='theAttrName' placeholder='请输入内容'  class='layui-textarea'></textarea>\n"+
						   "\t\t\t\t\t\t\t</div>\n"+
						   "\t\t\t\t\t\t</div>\n";
		String none =  "\t\t\t\t\t\t<div class='layui-form-item'>\n"+
					   "\t\t\t\t\t\t</div>\n";
		
		String result = "";
		
		if(ele instanceof SingleEle){
			SingleEle single = (SingleEle)ele;
			switch(single.type){
				case "input":
					result = singleEle;
					result = result.replaceAll("theName", single.name);
					result = result.replaceAll("theAttrName", single.attrName);
					return result;
				case "hide":
					result = hide;
					result = result.replaceAll("theName", single.name);
					result = result.replaceAll("theAttrName", single.attrName);
					return result;
				case "date":
					result = date;
					result = result.replaceAll("theName", single.name);
					result = result.replaceAll("theAttrName", single.attrName);
					return result;
				case "select":
					
					result = select;
					String optionClass = single.optionClass.substring(0, 1).toLowerCase() + single.optionClass.substring(1);
					result = result.replaceAll("theName", single.name);
					result = result.replaceAll("theAttrName", single.attrName);
					result = result.replaceAll("optionId", single.optionId);
					result = result.replaceAll("optionValue", single.optionValue);
					result = result.replaceAll("optionClassBig", single.optionClass);
					result = result.replaceAll("optionClass", optionClass);
					
					StringBuffer condition = new StringBuffer();
					condition.append("[");
					for(String c : single.condition.split(",")){
						condition.append('"'+c+'"'+',');
					}
					condition = new StringBuffer(condition.substring(0,condition.lastIndexOf(",")));
					condition.append("]");
					result = result.replaceAll("condition", condition.toString());
					
					return result;
				case "textarea":
					result = textarea;
					result = result.replaceAll("theName", single.name);
					result = result.replaceAll("theAttrName", single.attrName);
					return result;
				case "null":
					return none;
			}
			
		}else if(ele instanceof DoubleEle){
			DoubleEle doub = (DoubleEle)ele;
			result = doubleEle;
			result = result.replaceAll("doubleLeft",OneKeyMakeForm.getEle(doub.singleLeft));
			result = result.replaceAll("doubleRight",OneKeyMakeForm.getEle(doub.singleRight));
			return result;
			
		}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
		
	}
	
	private static void setJetx(List<Object> paramList){
		try {
			
			String jetx = OneKeyMakeForm.getFileString(jetxUrl);
			
			StringBuffer theDemoForm = new StringBuffer();
			
			for(Object  o: paramList){
				String ele = OneKeyMakeForm.getEle(o);
				theDemoForm.append(ele);
			}
			
			jetx = jetx.replaceAll("demoProject", project);
			jetx = jetx.replaceAll("demoModule", module);
			jetx = jetx.replaceAll("demo", key);
			jetx = jetx.replaceAll("theDemoForm", theDemoForm.toString());
			
			
			OneKeyMakeForm.writeFile( jetxConfigUrl+"/"+project+"/"+module+"/"+key+".jetx", jetx);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/** 
	* @author 卢俊华 
	* @version 创建时间：2018年8月9日 上午9:14:37 
	* @Description 设置主键
	* @param
	*/ 
	private static Boolean setKey(List<Object> paramList){
		
		for(Object ele : paramList){
			if(ele instanceof SingleEle){
				SingleEle single = (SingleEle)ele;
					if(single.isKey){
						theKey = single.attrName;
						return true;
					}
			}else if(ele instanceof DoubleEle){
				
				DoubleEle doub = (DoubleEle)ele;
				if(doub.singleLeft.isKey){
					theKey = doub.singleLeft.attrName;
					return true;
				}else if(doub.singleLeft.isKey){
					theKey = doub.singleRight.attrName;
					return true;
				}
			}
		}
		
		return false;
		
	}
	
	private static void setJs(Class student,List<Object> paramList,String ...value){
		try {
			
			Field[] keys = student.getDeclaredFields();
			StringBuffer sb = new StringBuffer();
			StringBuffer attrSB = new StringBuffer();
			String templet = "";
			int i = 0;
			int width = 80/keys.length;
			
			
			for(Field key:keys){
				
				//根据属性设置列
				if(key.getName().equals("id")){
					 templet = "   {\n"+
								    	  "\t\t\t\t\t\t\t\t title: '"+(i>=value.length?key.getName():value[i++])+"',\n"+ 
								    	  "\t\t\t\t\t\t\t\t width:'"+width+"%',\n"+
								    	  "\t\t\t\t\t\t\t\t sort: true,\n"+
								    	  "\t\t\t\t\t\t\t\t fixed: true,\n"+
								    	  "\t\t\t\t\t\t\t\t\t\t templet: function(res){\n"+
								    	  "\t\t\t\t\t\t\t\t\t\treturn res['LAY_INDEX'];\n"+
								    	  "\t\t\t\t\t\t\t\t\t} \n"+
								          "\t\t\t\t\t\t\t\t},\n";
				}else{
					if(key.getGenericType().toString().equals("class java.util.Date")){
						//如果是时间类型
						templet =   "\t\t\t\t\t\t\t\t{\n"+
									"\t\t\t\t\t\t\t\t title: '"+(i>=value.length?key.getName():value[i++])+"',\n"+ 
									"\t\t\t\t\t\t\t\t width:'"+width+"%',\n"+
									"\t\t\t\t\t\t\t\t\t\t templet: function(data){\n"+
									"\t\t\t\t\t\t\t\t\t\t if(data."+key.getName()+"!=null){\n"+
						    		"\t\t\t\t\t\t\t\t\t\t return data."+key.getName()+".substring(0,10);\n"+
						    		"\t\t\t\t\t\t\t\t\t\t }else{\n"+
						    		"\t\t\t\t\t\t\t\t\t\t return '';\n"+
						    		"\t\t\t\t\t\t\t\t\t\t }\n"+
									"\t\t\t\t\t\t\t\t\t\t return data."+key.getName()+";\n"+
									"\t\t\t\t\t\t\t\t\t} \n"+
									"\t\t\t\t\t\t\t\t},\n";
					}else{
						templet =   "   \t\t\t\t\t\t\t\t{\n"+
									"\t\t\t\t\t\t\t\t title: '"+(i>=value.length?key.getName():value[i++])+"',\n"+ 
									"\t\t\t\t\t\t\t\t width:'"+width+"%',\n"+
									"\t\t\t\t\t\t\t\t\t\t templet: function(data){\n"+
									"\t\t\t\t\t\t\t\t\t\t return data."+key.getName()+";\n"+
									"\t\t\t\t\t\t\t\t\t} \n"+
									"\t\t\t\t\t\t\t\t},\n";
					}
				}
				sb.append(templet);
				
				//设置弹出框的属性
				if(key.getGenericType().toString().equals("class java.util.Date")){
					attrSB.append("\t\t\t\t\t\t\t\t'"+key.getName()+"' :msg."+key.getName()+"==null?null:msg."+key.getName()+".format('yyyy-MM-dd')"+",\n");
				}else{
					attrSB.append("\t\t\t\t\t\t\t\t'"+key.getName()+"'  :msg==null?null:msg."+key.getName()+",\n");
				}
			}
			
			List<SingleEle> timeList = new ArrayList<SingleEle>(); 
			//设置时间插件代码
			for(Object ele : paramList){
				if(ele instanceof SingleEle){
					SingleEle single = (SingleEle)ele;
					if(single.type.equals("date")){
						timeList.add(single);
					}
				}else if(ele instanceof DoubleEle){
					DoubleEle doub = (DoubleEle)ele;
					if(doub.singleLeft.type.equals("date")){
						timeList.add(doub.singleLeft);
					}
					if(doub.singleRight.type.equals("date")){
						timeList.add(doub.singleRight);
					}
				}
			}
			StringBuffer timeSB = new StringBuffer();
			for(SingleEle single : timeList){
				
				    
				 String t  =  "\t\t\t\t\t\t\t\t//"+single.name+"时间\n"+
					    	  "\t\t\t\t\t\t\t\t layui.laydate.render({\n"+ 
					    	  "\t\t\t\t\t\t\t\t elem: layero.find('#"+single.attrName+"')[0]//解决日期闪一下就消失的问题\n"+
					    	  "\t\t\t\t\t\t\t\t\t\t ,format: 'yyyy-MM-dd'\n"+
					    	  "\t\t\t\t\t\t\t\t });\n";
				 timeSB.append(t);
				
			}
			
			String js = OneKeyMakeForm.getFileString(jsUrl);
			
			
			js = js.replaceAll("TheField", sb.toString());
			js = js.replaceAll("demo", key);
			js = js.replaceAll("Demo", bigKey);
			js = js.replaceAll("searchText", searchText);
			js = js.replaceAll("theTimeCode", timeSB.toString());
			js = js.replaceAll("theAttrCode", attrSB.toString());
			js = js.replaceAll("thePrimaryKey", theKey);
			
			
			OneKeyMakeForm.writeFile( styleConfigUrl+"/"+project+"/"+module+"/js/"+key+".js", js);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void setCss(){
		try {
			
			String css = OneKeyMakeForm.getFileString(cssUrl);
			
			OneKeyMakeForm.writeFile( styleConfigUrl+"/"+project+"/"+module+"/css/"+key+".css", css);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private static void setController(){
		try {
			
			String controllerText = OneKeyMakeForm.getFileString(controllerUrl);
			
			String sText  =   "\t\t\t String searchText = actionValueAssistant.getString(\"searchText\");\n"+
							  "\t\t\t if(searchText!=null && !searchText.equals(\"\")){\n"+
					    	  "\t\t\t hql.append(\" and searchText like ? \");\n"+ 
					    	  "\t\t\t param.add('%'+searchText+'%');\n"+
					    	  "\t\t\t }\n";
					
			controllerText = controllerText.replaceAll("demoProject", project);
			controllerText = controllerText.replaceAll("demoModule", module);
			controllerText = controllerText.replaceAll("demo", key);
			controllerText = controllerText.replaceAll("Demo", bigKey);
			
			if(searchText!=null && !searchText.equals("")){
				sText = sText.replaceAll("searchText", searchText);
				controllerText = controllerText.replaceAll("searchText", sText);
			}
			String controllerName = controller.equals("")==true?"":controller.substring(0, 1).toUpperCase() + controller.substring(1);
			String controllerUrl = coreConfigUrl+"/web/mvc/action/impl/"+controllerName+".java"; 
			
			//如果文件已经存在，那就在后面追加，不存在就新建
			if(new File(controllerUrl).exists()){
				OneKeyMakeForm.appendFile(controllerUrl);
			}else{
				OneKeyMakeForm.writeFile(controllerUrl , controllerText);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void setManager(){
		try {
			
			String manager = OneKeyMakeForm.getFileString(managerUrl);
			
			manager = manager.replaceAll("demoProject", project);
			manager = manager.replaceAll("demo", key);
			manager = manager.replaceAll("Demo", bigKey);
			
			OneKeyMakeForm.writeFile( coreConfigUrl+"/server/manager/impl/"+bigKey+"Manager.java", manager);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void setService(){
		try {
			
			String service = OneKeyMakeForm.getFileString(serveiceUrl);
			
			service = service.replaceAll("demoProject", project);
			service = service.replaceAll("demo", key);
			service = service.replaceAll("Demo", bigKey);
			
			OneKeyMakeForm.writeFile( coreConfigUrl+"/server/service/"+bigKey+"Service.java", service);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	
	
	
//	
//	public void createFile(){
//		project = "xmgl";//项目名
//		module = "studentManager";//模块名
//		key = "student";//功能名称
//		bigKey = key.substring(0, 1).toUpperCase() + key.substring(1);
//		searchText = "name";//搜索的属性，要是类里面的属性
//		
//		//添加数据的表单属性设置
//		SingleEle id = new SingleEle("id", "id", "hide",true);
//		SingleEle name = new SingleEle("姓名", "name", "input");
//		DoubleEle doubleEle2 = new DoubleEle(new SingleEle("类型", "type", "input"),new SingleEle("", "", "null"));
//		DoubleEle doubleEle = new DoubleEle(new SingleEle("科目", "subject", "select", "Business","id","name", "status = 0"), new SingleEle("时间", "mageUrl", "date"));
//		SingleEle introduce = new SingleEle("简介", "introduce", "textarea");
//		
//		
//		
//		List<Object> paramList = new ArrayList<Object>(){
//			{
//				add(id);
//				add(name);
//				add(doubleEle2);
//				add(doubleEle);
//				add(introduce);
//			}
//		};
//
//		
//		OneKeyMakeForm.setJetx(paramList);
//		OneKeyMakeForm.setJs(Student.class,paramList,"序号","姓名","自我介绍","入学时间","类型","科目");
//		OneKeyMakeForm.setCss();
//		OneKeyMakeForm.setController();
//		OneKeyMakeForm.setManager();
//		OneKeyMakeForm.setService();
//
//		System.out.println("创建完成！地址：http://localhost:8888/"+key);
//		System.out.println("需要刷新项目和重启服务器才有效");
//		System.out.println(FilePath.getSystemRootPath());
//		System.out.println(FilePath.getSystemRootPath().replace("/war", ""));
//	}
	
	
	
	public static void createFile(Map<String,Object> data) throws ClassNotFoundException{
		
		//设置url
		coreConfigUrl =  (String) data.get("coreUrl");
		jetxConfigUrl =  (String) data.get("jetxUrl");
		styleConfigUrl =  (String) data.get("styleUrl");
		
		
		project = (String) data.get("projectName");//项目名
		module = (String) data.get("module");//模块名
		key = (String) data.get("key");//功能名称
		bigKey = key.substring(0, 1).toUpperCase() + key.substring(1);
		controller = (String) data.get("controller")==""?bigKey:((String) data.get("controller")).substring(0, 1).toUpperCase() + ((String) data.get("controller")).substring(1);//controller名称
		searchText = (String) data.get("searchText");//搜索的属性，要是类里面的属性
		
		List eleList = (List) data.get("eleList");
		
		
		List<Object> paramList = new ArrayList<Object>();
		//添加数据的表单属性设置
		OneKeyMakeForm one = new OneKeyMakeForm();
		if(eleList!=null &&  eleList.size()>0){
			for(Object ele: eleList){
				//ele可能是Map，也可能是List<Map>
				if(ele instanceof Map){
					Map<String,Object> map = (Map<String,Object>)ele;
					SingleEle single = one.getSingle((String)map.get("name"),(String)map.get("attrName"), (String)map.get("type"), (String)map.get("optionClass"), String.valueOf(map.get("optionId")), (String)map.get("optionValue"), (String)map.get("condition"),(Boolean)map.get("isKey"));
					paramList.add(single);
				}else if(ele instanceof List){
					List<Map> eList = (List<Map>)ele;
					List<SingleEle> douList = new ArrayList<SingleEle>();
					for(Map e : eList){
						Map<String,Object> map = (Map<String,Object>)e;
						
						SingleEle single = one.getSingle((String)map.get("name"),(String)map.get("attrName"), (String)map.get("type"), (String)map.get("optionClass"), String.valueOf(map.get("optionId")), (String)map.get("optionValue"), (String)map.get("condition"),(Boolean)map.get("isKey"));
						douList.add(single);
					}
					DoubleEle dou = one.getDouble(douList.get(0), douList.get(1));
					paramList.add(dou);
				}
			}
		}
		
		//获取所有属性
		Class clazz = Class.forName("org."+project+".core.server.model.impl."+bigKey);
		Field[] fields = clazz.getDeclaredFields();
		String[] attrName = new String[fields.length];
		for(int i = 0; i < fields.length ; i++){
			System.out.println(fields[i].getName());
			String name = (String) data.get("table_"+fields[i].getName().toString());
			attrName[i] = name;
		}
		

		OneKeyMakeForm.setKey(paramList);
		OneKeyMakeForm.setJetx(paramList);
		OneKeyMakeForm.setJs(clazz,paramList,attrName);
		OneKeyMakeForm.setCss();
		OneKeyMakeForm.setController();
		OneKeyMakeForm.setManager();
		OneKeyMakeForm.setService();

		System.out.println("创建完成！地址：http://localhost:8888/"+key);
		System.out.println("需要刷新项目和重启服务器才有效");
		System.out.println(FilePath.getSystemRootPath());
		System.out.println(FilePath.getSystemRootPath().replace("/war", ""));
	}
	
	public static void main(String[] args) {
//		OneKeyMakeForm o = new OneKeyMakeForm();
//		o.createFile();
		
		
		
//		String single =  "status = 0,id = 2";
//		
//		StringBuffer condition = new StringBuffer();
//		condition.append("[");
//		for(String c : single.split(",")){
//			condition.append('"'+c+'"'+',');
//		}
//		condition = new StringBuffer(condition.substring(0,condition.lastIndexOf(",")));
//		condition.append("]");
//		
//		System.out.println(condition.toString());
		
		project = "xmgl";
		bigKey = "Icon";
		key = "icon";

	
	}

	
	
	
	
	
	
	
	
	
	
	
	

}
